package org.dashboard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.dashboard.bean.AdDataset;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdDatasetService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dataset/dataset")
public class AdDatasetController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdDatasetController.class);

    @Reference
    private AdDatasetService adDatasetService;


    @RequestMapping(value = "/getlist", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse getlist(HttpServletRequest request) {
        Integer schemaId = Integer.parseInt(request.getParameter("schemaId"));

        List<AdDataset> schemas = adDatasetService.selectAdDatasetsBySchemaId(schemaId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.listToListMap(schemas));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse get(HttpServletRequest request) {

        List<AdDataset> adDatasets = adDatasetService.selectAdDatasets();
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.listToListMap(adDatasets));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse search(HttpServletRequest request) {
        String criteria = request.getParameter("criteria");
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        PageInfo pageInfo = adDatasetService.selectAdDatasetsPaged(criteria, pageIndex, pageSize);

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(pageInfo.getList()));
        map.put("count", pageInfo.getTotal());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        String datasetIds = request.getParameter("datasetIds");
        JSONArray jsonArray = (JSONArray) JSON.parse(datasetIds);
        ArrayList<Integer> ids = new ArrayList<>();
        if (jsonArray != null) {
            int len = jsonArray.size();
            for (int i = 0; i < len; i++) {
                ids.add(jsonArray.getInteger(i));
            }
        }
        adDatasetService.deleteByPrimaryKeys(ids);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/load", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse load(HttpServletRequest request) {

        String datasetId = request.getParameter("datasetId");
        String datasetCode = request.getParameter("datasetCode");

        AdDataset dataset;

        if (!StringUtil.isEmpty(datasetId)) {
            dataset = adDatasetService.selectByPrimaryKey(Integer.parseInt(datasetId));
        } else if (!StringUtil.isEmpty(datasetCode)) {
            dataset = adDatasetService.selectAdDatasetByCode(datasetCode);
        } else {
            return JsonResponse.failed();
        }

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(dataset));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/check", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse check(HttpServletRequest request) {
        Integer datasetId = Integer.parseInt(request.getParameter("datasetId"));
        String datasetCode = request.getParameter("datasetCode");

        Integer existCount = adDatasetService.checkDatasetCodeExist(datasetCode, datasetId);
        return existCount == 0 ? JsonResponse.success() : JsonResponse.failed();
    }


    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update(HttpServletRequest request) {
        Integer datasetId = Integer.parseInt(request.getParameter("datasetId"));
        Integer schemaId = Integer.parseInt(request.getParameter("schemaId"));
        String datasetCode = request.getParameter("datasetCode");
        String datasetName = request.getParameter("datasetName");
        String datasetData = request.getParameter("datasetData");

        AdDataset adDataset = new AdDataset();
        adDataset.setDatasetId(datasetId);
        adDataset.setSchemaId(schemaId);
        adDataset.setDatasetCode(datasetCode);
        adDataset.setDatasetName(datasetName);
        adDataset.setDatasetData(datasetData);

        adDatasetService.updateByPrimaryKeySelective(adDataset);

        adDataset = adDatasetService.selectByPrimaryKey(datasetId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adDataset));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        Integer schemaId = Integer.parseInt(request.getParameter("schemaId"));
        String datasetCode = request.getParameter("datasetCode");
        String datasetName = request.getParameter("datasetName");
        String datasetData = request.getParameter("datasetData");

        AdDataset adDataset = new AdDataset();
        adDataset.setSchemaId(schemaId);
        adDataset.setDatasetCode(datasetCode);
        adDataset.setDatasetName(datasetName);
        adDataset.setDatasetData(datasetData);

        adDataset.setDatasetId(adDatasetService.insert(adDataset));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adDataset));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }



}
